package class04;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 正整数 costs 正整数数组profits  正数k 正数M
 * costs[i] 表示项目的花费
 * profits[i] 表示项目纯利润
 * k 表示能做的项目数
 * m 表示初始资金
 */
public class Code07_IPO {

    public static class Program {
        public int p;
        public int c;

        public Program(int p, int c) {
            this.p = p;
            this.c = c;
        }
    }

    /**
     * 准备大根堆存放利润
     * 准备小根堆存放话费
     *
     * @param K
     * @param W
     * @param Profits
     * @param Capital
     * @return
     */
    public static int findMaximizedCapital(int K, int W, int[] Profits, int[] Capital) {
        PriorityQueue<Program> cPriorityQueue = new PriorityQueue<>(new MinCostComparator());
        PriorityQueue<Program> pPriorityQueue = new PriorityQueue<>(new MaxProfitComparator());

        for (int i = 0; i < Profits.length; i++) {
            cPriorityQueue.add(new Program(Profits[i], Capital[i]));
        }
        int ans;
        for (int i = 0; i < K; i++) {

            while (cPriorityQueue.isEmpty() && W >= cPriorityQueue.peek().c) {
                pPriorityQueue.add(cPriorityQueue.poll());

            }
            // 确实做不到K 个项目，提前结束
            if (pPriorityQueue.isEmpty()) {
                return W;
            }
            W += pPriorityQueue.poll().p;
        }
        return W;
    }

    static class MinCostComparator implements Comparator<Program> {

        @Override
        public int compare(Program o1, Program o2) {
            return o1.c - o2.c;
        }
    }

    public static class MaxProfitComparator implements Comparator<Program> {

        @Override
        public int compare(Program o1, Program o2) {
            return o2.p - o1.p;
        }

    }

}
